<template>
  <div class="app-container" style="">
    <div style="width: 50%">
      <h2 style="text-align: center">发布新课程</h2>
      <el-steps
        :active="1"
        process-status="wait"
        align-center
        style="margin-bottom: 40px"
      >
        <el-step title="填写课程基本信息" />
        <el-step title="创建课程大纲" />
        <el-step title="提交审核" />
      </el-steps>

      <el-form label-width="120px">
        <el-form-item label="课程标题">
          <el-input
            v-model="courseInfo.title"
            placeholder=" 示例：机器学习项目课：从基础到搭建项目视频课程。专业名称注意大小写"
          />
        </el-form-item>

        <!-- 所属分类 TODO -->
        <el-form-item label="课程类别">
          <el-select
            @change="subjectLevelOneChanged"
            v-model="courseInfo.subjectParentId"
            placeholder="请选择"
          >
            <el-option
              v-for="subject in rootSubjectList"
              :key="subject.id"
              :label="subject.title"
              :value="subject.id"
            />
          </el-select>
          <el-select v-model="courseInfo.subjectId" placeholder="请选择">
            <el-option
              v-for="subject in childrenSubjectList"
              :key="subject.value"
              :label="subject.title"
              :value="subject.id"
            />
          </el-select>
        </el-form-item>

        <!-- 课程讲师 TODO -->
        <!-- 课程讲师 -->
        <el-form-item label="课程讲师">
          <el-select v-model="courseInfo.teacherId" placeholder="请选择">
            <el-option
              v-for="teacher in teacherList"
              :key="teacher.id"
              :label="teacher.name"
              :value="teacher.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="总课时">
          <el-input-number
            :min="0"
            v-model="courseInfo.lessonNum"
            controls-position="right"
            placeholder="请填写课程的总课时数"
          />
        </el-form-item>

        <!-- 课程简介 TODO -->
        <el-form-item label="课程简介">
          <tinymce :height="300" v-model="courseInfo.description" />
        </el-form-item>

        <!-- 课程封面-->
        <el-form-item label="课程封面">
          <el-upload
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            :action="BASE_API + '/eduoss/fileoss/upload'"
            class="avatar-uploader"
          >
            <img class="cover" :src="courseInfo.cover" />
          </el-upload>
        </el-form-item>

        <el-form-item label="课程价格">
          <el-input-number
            :min="0"
            v-model="courseInfo.price"
            controls-position="right"
            placeholder="免费课程请设置为0元"
          />
          元
        </el-form-item>

        <el-form-item>
          <el-button :disabled="saveBtnDisabled" type="primary" @click="next"
            >保存并下一步</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
  <script>
import course from "@/api/course";
import subject from "@/api/subject";

import Tinymce from "@/components/Tinymce";
export default {
  components: {
    Tinymce,
  },
  data() {
    return {
      BASE_API: process.env.BASE_API, // 接口API地址
      saveBtnDisabled: false, // 保存按钮是否禁用
      courseInfo: {
        title: "",
        subjectId: "",
        subjectParentId: "",
        teacherId: "",
        lessonNum: 0,
        description: "",
        cover: "/static/defaultCover.webp",
        price: 0,
      },
      teacherList: [],
      rootSubjectList: [], //一级分类列表
      childrenSubjectList: [], //二级分类列表
    };
  },

  created() {
    this.getTeacherList();
    this.init();
  },
  methods: {
    //回显课程信息
    getCourseList(courseId) {
      course.getCourseInfo(courseId).then((response) => {
          this.courseInfo = response.data.items;
          subject.getAllSubject().then((response) => {
            this.rootSubjectList = response.data.subjectList;
            for (let i = 0; i < this.rootSubjectList.length; i++) {
              if (
                this.rootSubjectList[i].id === this.courseInfo.subjectParentId
              ) {
                this.childrenSubjectList = this.rootSubjectList[i].children;
              }
            }
          });
          this.getTeacherList();
        })
        .catch((response) => {
          this.$message({
            type: "error",
            message: response.message,
          });
        });
    },


    init() {
      // 初始化分类列表
      this.initSubjectList();
      if (this.$route.params && this.$route.params.id) {
        const courseId = this.$route.params.id;
        // 根据id获取课程基本信息
        this.getCourseList(courseId);
      } else {

      }
    },
    //初始化一级分类
    initSubjectList() {
      subject.getAllSubject().then((response) => {
        this.rootSubjectList = response.data.subjectList;
      });
    },
    //随以及分类改变展示二级分类
    subjectLevelOneChanged(value) {
      for (let i = 0; i < this.rootSubjectList.length; i++) {
        if (this.rootSubjectList[i].id === value) {
          this.childrenSubjectList = this.rootSubjectList[i].children;
          this.courseInfo.subjectId = "";
        }
      }
    },
    //获取讲师列表
    getTeacherList() {
      course.teacherListInfo().then((response) => {
        this.teacherList = response.data.items;
      });
    },
    //修改讲师信息
    updateCourseInfo() {
      course.updateCourseInfo(this.courseInfo).then((response) => {
        
          this.$message({
            showClose: true,
            message: "课程保存成功",
            type: "success",
          });
        this.$router.push({
          path: `/edu/course/chapter/${this.courseInfo.id}`,
        });
      });
    },
    //保存僵尸信息
    saveCourserInfo() {
      course.saveCourseInfo(this.courseInfo).then((response) => {
        console.log(response.data);
          this.$message({
            showClose: true,
            message: "课程添加成功",
            type: "success",
          });
        this.$router.push({
          path: `/edu/course/chapter/${response.data.courseId}`,
        });
      });
    },
    //保存下一页
    next() {
      if (this.$route.params && this.$route.params.id) {
        this.updateCourseInfo();
      } else {
        this.saveCourserInfo();
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG/PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    handleAvatarSuccess(res, file) {
      this.courseInfo.cover = res.data.url;
    },
  },
};
</script>
<style scoped>
.tinymce-container {
  line-height: 29px;
}
.cover {
  width: 350px;
  height: 350px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 20px;
}
</style>